﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private string username;
        private string power;
        private DateTime logonTime;

        public void setUsername(string username)
        {
            this.username = username;
        }
        public void setPower(string power)
        {
            this.power = power;
        }
        public void setLogonTime(DateTime logonTime)
        {
            this.logonTime = logonTime;
        }
        public Form2()
        {
            InitializeComponent();
        }

        //初始化缓存
        private void initRooms()
        {
            RuningData.Rooms.Clear();
            string sql = "SELECT * FROM tb_Room";
            SqlDataReader sdr = DatabaseHelper.GetSqlDataReader(sql);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    //封装对象保存在缓存中
                    RoomModel rm = new RoomModel();
                    rm.ID = Convert.ToInt32(sdr["ID"].ToString().Trim());
                    rm.RoomName = sdr["RoomName"].ToString().Trim();
                    rm.RoomJC = sdr["RoomJC"].ToString().Trim();
                    rm.RoomBJF = Convert.ToInt32(sdr["RoomBJF"].ToString().Trim());
                    rm.RoomWZ = sdr["RoomWZ"].ToString().Trim();
                    rm.RoomZT = sdr["RoomZT"].ToString().Trim();
                    rm.RoomType = sdr["RoomType"].ToString().Trim();
                    RuningData.Rooms.Add(rm);
                }
            }
            sdr.Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (!RuningData.FormDir.ContainsKey(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name))
            {
                RuningData.FormDir.Add(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, this);
            }
            toolStripStatusLabel2.Text = username;
            toolStripStatusLabel4.Text = returnPowerName(power.Trim());
            toolStripStatusLabel6.Text = logonTime.ToString();
            listView1.LargeImageList = imageList1;
            if(RuningData.Rooms.Count == 0)
            {
                this.initRooms();
            }
            this.flushRoomImage();
        }
        private string returnPowerName(string power)
        {
            string returnPowerName = null;
            switch (power)
            {
                case "0": { returnPowerName = "数据库管理员"; }; break;
                case "1": { returnPowerName = "大堂经理"; }; break;
                case "2": { returnPowerName = "普通员工"; }; break;
            }
            return returnPowerName;
        }

        private void 数据库管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        public void flushRoomImage()
        {
            listView1.Clear();
            foreach (RoomModel item in RuningData.Rooms)
            {
                string zt = item.RoomZT;
                if (zt.Trim().Equals("待用"))
                {
                    listView1.Items.Add(item.RoomName+"-"+item.ID, 0);
                }
                else
                {
                    listView1.Items.Add(item.RoomName + "-" + item.ID, 1);
                }
            }
        }

        private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void 开桌ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            //设置右击打开菜单时触发的监听事件
            if (listView1.SelectedItems.Count != 0)
            {
                contextMenuStrip1.Items[0].Enabled = true;
            }
            else
            {
                contextMenuStrip1.Items[0].Enabled = false;
            }
        }

        private void xXXXXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GLForm glf = new GLForm();
            glf.Show();
            this.Hide();
        }
    }
}
